package com.hainatong.shiphelper.module.warehouse.viewmodel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blankj.utilcode.util.StringUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.HttpResult;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.mainhome.entity.WhereComBean;
import com.hainatong.shiphelper.module.warehouse.entity.SupplierBean;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.PageLiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class SupplierViewModel extends BaseViewModel
{
    public static int PAGE_SIZE = 50;


    private int page;

    private boolean isRefresh = false;

    private boolean isHasNextPage = false;

    private String keyWord;

    public SupplierViewModel(@NonNull Application application)
    {
        super(application);
    }

    private MutableLiveData<PageLiveResult<List<SupplierBean>>> supplierResult = new MutableLiveData<>();

    public MutableLiveData<PageLiveResult<List<SupplierBean>>> getSupplierResult()
    {
        return supplierResult;
    }

    /**
     *  查 供应商
     */
    private void getSupplierByParam( int mPage, String portKeyName)
    {
        Map<String,Object> param = new HashMap<String,Object>();
        param.put(Constants.Key.PAGE, String.valueOf( mPage ));
        param.put( Constants.Key.ROWS, String.valueOf( PAGE_SIZE ));
        List<WhereComBean> whereList = new ArrayList<>();
        whereList.add( new WhereComBean( "name", portKeyName) );
        //whereList.add( new WhereComBean( "is_deleted", "0" ) );
        param.put( "wheres", GsonUtil.obj2json( whereList ));
        if(!StringUtils.isEmpty( portKeyName ))
        {
            param.put( "wheres", GsonUtil.obj2json( whereList ));
        }

        ApiService.Builder.getChanBoServer()
                .getSupplierList( param)
                .compose( RxSchedulersUtils.Obs_io_main() )
                .subscribe(new VMObserver<HttpResult<List<SupplierBean>>>()
                {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        PageLiveResult<List<SupplierBean>> errorResult = new PageLiveResult<>();
                        errorResult.setSuccess(false);
                        errorResult.setRefresh(isRefresh);
                        errorResult.setMsg(errorMsg);
                        if( !isRefresh )
                        {
                            errorResult.setLoadMoreEmpty(true);
                        }
                        getSupplierResult().postValue( errorResult );
                    }

                    @Override
                    public void onSuccess(HttpResult<List<SupplierBean>> result)
                    {
                        if( result.isOk() )
                        {
                            PageLiveResult<List<SupplierBean>> listResult = new PageLiveResult<>();
                            listResult.setSuccess(true);
                            listResult.setRefresh(isRefresh);
                            if( result.getRows() != null && !result.getRows().isEmpty() )
                            {
                                listResult.setData( result.getRows() );
                                if( result.getRows().size() < PAGE_SIZE )
                                {
                                    isHasNextPage = false;
                                }
                                else
                                {
                                    isHasNextPage = true;
                                    page = page + 1;
                                }
                            }
                            getSupplierResult().postValue(listResult);
                        }
                        else
                        {
                            PageLiveResult<List<SupplierBean>> failResult = new PageLiveResult<>();
                            failResult.setSuccess(false);
                            failResult.setRefresh(isRefresh);
                            failResult.setMsg(result.getMsg());
                            if( !isRefresh )
                            {
                                failResult.setLoadMoreEmpty(true);
                            }
                            getSupplierResult().postValue(failResult);
                        }
                    }
                });


    }

    /**
     *  查询  供应商
     */
    public void querySupplier(String keyPort)
    {
        this.page = 1;
        this.isRefresh = true;
        this.isHasNextPage = true;
        this.keyWord = keyPort;
        getSupplierByParam( this.page,keyPort );
    }

    public void tryRefresh()
    {
        querySupplier(this.keyWord);
    }

    public void loadMore()
    {
        if( isHasNextPage )
        {
            this.isRefresh = false;
            getSupplierByParam(this.page,this.keyWord);
        }
        else
        {
            PageLiveResult<List<SupplierBean>> emptyResult = new PageLiveResult<>();
            emptyResult.setSuccess(false);
            emptyResult.setLoadMoreEmpty(true);
            emptyResult.setRefresh(false);
            getSupplierResult().postValue(emptyResult);
        }
    }


}
